﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClientWPF.localhost;

namespace ClientWPF
{
    public partial class RegisterForm : Window
    {
        Service1 service = new Service1();

        public RegisterForm()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            User user = new User();
            user.Name = NameBox.Text;
            if (!service.CheckUserExist(user))
            {
                if (PasswordBox.Password.Equals(RepeatPasswordBox.Password))
                {
                    if (!(PasswordBox.Password.Equals("") || NameBox.Text.Equals("")))
                    {
                        if (!((PasswordBox.Password.Length > 30) || (NameBox.Text.Length > 30)))
                        {
                            user.Password = PasswordBox.Password;
                            service.RegisterUser(user);
                            MessageBox.Show("User " + user.Name + " added successfully!");
                            var login = new MainWindow();
                            Close();
                            login.Show();
                        }
                        else MessageBox.Show("The length of name and password cannot be longer than 30 characters!");
                    }
                    else MessageBox.Show("One or more fields are blanc!");
                }
                else MessageBox.Show("Passwords do not match!");
            }
            else MessageBox.Show("User " + user.Name + " already exists!");
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            var form = new MainWindow(); //LogInForm
            Close();
            form.Show();
        }
    }
}
